

package com.base.commons.tools.utils;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;

/**
 * 国际化
 *
 * @author carl
 * @since 1.0.0
 */
public class MessageUtils {
    private static MessageSource messageSource;
    static {
        messageSource = (MessageSource)SpringContextUtil.getBean("messageSource");
    }

    public static String getMessage(int code){
        return getMessage(code, new String[0]);
    }

    public static String getMessage(String code){
        return getMessage(code, new String[0]);
    }



    public static String getMessage(int code, String... params){
        try {
            return messageSource.getMessage(code + "", params, LocaleContextHolder.getLocale());
        } catch (Exception e) {
            try{
                return messageSource.getMessage(code + "", params, Locale.SIMPLIFIED_CHINESE);
            }catch (Exception eq){
                return  null;
            }

        }

    }
    /*如果是枚举类型走占位符*/
    public static String getMessage(String code, String... params){
        try{
            return messageSource.getMessage(code, params, LocaleContextHolder.getLocale());
        }catch (Exception e){
            try{
                return messageSource.getMessage(code, params, Locale.SIMPLIFIED_CHINESE);
            }catch (Exception eq){
                return  null;
            }


        }
    }

}
